<?php
namespace app\backstage\controller;

use app\model\Product;
use think\Db;
use app\model\CommonClass as Model;
class CommonClass extends Base
{
    //获取分类列表
    public function index() {
        $page = $this->request->get('page', 1);
        $size = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $where = [];
        if(isset( $this->request->get()['type'])){
            $where['type'] =  $this->request->get()['type'];
        }
        if(isset( $this->request->get()['status'])){
            $where['status'] =  $this->request->get()['status'];
        }
        $list = Db::name('class')->where($where)->order('sort asc')->limit(( $page - 1 ) * $size , $size)->select();
        $count = Db::name('class')->where($where)->count();
//        $count = count($list);
        $new_list = [];
        foreach ($list as $k => $val){
            $status = ['0' => '未启用','1' => '已启用'];
            $type = ['0' => '产品分类','1' => '商家分类','2' => '商超分类','3' => '商城分类'];
            $val['statusText'] = $status[$val['status']];
            $val['typeText'] = $type[$val['type']];
            $val['addTime'] = date('Y-m-d H:i:s', $val['addTime']);
            unset($val['updataTime']);
            if ($val['parentID'] == 0){
                $new_list[$k] = $val;
                $new_list[$k]['child'] = [];
            }
           foreach ($new_list as $key => $value){
                if ($value['classID'] == $val['parentID']){
                    $new_list[$key]['child'][] = $val;
                }
           }
        }
        $list = array_merge($new_list);
        //$arr = array_reverse($arr);
        return $this->buildSuccess([
            'list'=>$list,
            'count'=> $count,
        ]);
    }

    /*
     * @name 修改分类状态
     * @param status 0禁用 1启用
     * @param classID 分类ID
     */
    public function updateStatus(){
        $classID = $this->request->post('classID');
        $status = $this->request->post('status');

        $model = Model::where('classID',$classID)->update(['status' => $status]);
        if ($model){
            return $this->buildSuccess([]);
        }
        return $this->buildFailed(-2,'操作失败');
    }


    /*
     * @name 新增/修改分类
     * @param classID  分类ID
     * @param className  分类名称
     */
    public function changeClass(){
        $postData = $this->request->post();
        //不存在新增 存在更新
        if (isset($postData['classID']) && $postData['classID']){
            $model = Model::where('classID',$postData['classID'])->update([
                'className' => $postData['className'],
                'status' => $postData['status'],
                'parentID' => 0,
                'icon' => $postData['icon'],
                'color' => isset($postData['color']) ? $postData['color']: '#f5bf50',
                'type' => $postData['type'],
                'sort' => $postData['sort'],
                'imgUrl' => $postData['imgUrl']
            ]);
            if ($model !== false){
                return $this->buildSuccess([]);
            }else{
                return $this->buildFailed(-2,'操作失败');
            }
        }else{

            $model = Model::create([
                'className' => $postData['className'],
                'icon' => $postData['icon'],
                'color' => isset($postData['color']) ? $postData['color']: '#f5bf50',
                'status' => $postData['status'],
                'parentID' => 0,
                'type' => $postData['type'],
                'sort' => $postData['sort'],
                'imgUrl' => $postData['imgUrl']
            ]);
            if ($model){
                return $this->buildSuccess([]);
            }
            return $this->buildFailed(-2,'操作失败');

        }

    }



}